﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Usefulls;
using MyConnection;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using HtmlAgilityPack;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Repository;

namespace Hotel_Price
{
    public partial class frmOtelKarsilastirma : DevExpress.XtraEditors.XtraForm
    {
       
        DataTable data_Grid1;
        IRDataSource data_MNG, data_Link;
        WebClient wc;
        HtmlAgilityPack.HtmlDocument hd;
        HtmlAgilityPack.HtmlNodeCollection hncFiyat;
        public frmOtelKarsilastirma()
        {
            InitializeComponent();
            this.btnListele.ItemClick += btnListele_ItemClick;
            this.btnNew.Click += btnNew_Click;
            this.btnSave.Click += btnSave_Click;
            this.btnHotelLinkList.Click += btnHotelLinkList_Click;
            this.btnCancel.Click += btnCancel_Click;
            this.btnExit.ItemClick += btnExit_ItemClick;

            data_Grid1 = CreateTable();
            this.btnSave.Visible = false;

            ConditionValidationRule ctrlNotNull = new ConditionValidationRule();
            ctrlNotNull.ConditionOperator = ConditionOperator.IsNotBlank;
            ctrlNotNull.ErrorText = "Boş Bırakılamaz!!!";
            ctrlNotNull.ErrorType = ErrorType.Critical;
            dxValidationProvider1.SetValidationRule(txtMNG, ctrlNotNull);
            dxValidationProvider1.SetValidationRule(txtHotelName, ctrlNotNull);
            Temizleme(false, false);
            MyDBTools.myConnectionStr = "Data Source=localhost;Initial Catalog=MNG0504;Integrated Security=True";
          
        }

        void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Temizleme(false, false);
            btnNew.Focus();
        }

        void btnHotelLinkList_Click(object sender, EventArgs e)
        {
            frmHotelLinkList frm = new frmHotelLinkList();
            frm.ShowDialog();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            bool kontrol = false;
            int ID = -1;
            try
            {
                if (txtHotelName.Text.Trim()!="")
                {
                    data_MNG = new IRDataSource("Select * From THotel_Link Where inID=-1");
                    data_MNG.Add();
                    DataRowView cur_MNG = (DataRowView)data_MNG.bs.Current;
                    cur_MNG["stLink"] = txtMNG.Text.Trim();
                    cur_MNG["stName"] = txtHotelName.Text;
                    data_MNG.Save();
                    ID = MyDBTools.getTableID("THotel_Link"); 
                }

                DataRowView cur_Link;
                data_Link = new IRDataSource("Select * From THotel_Link_Control Where inID=-1");
                if ((txtJOL.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtJOL.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "JOL";
                    data_Link.Save(); 
                }


                if ((txtANI.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtANI.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "ANI";
                    data_Link.Save();
                }


                if ((txtTatilBudur.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtTatilBudur.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "TTB";
                    data_Link.Save();
                }

                if ((txtTouristica.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtTouristica.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "TTC";
                    data_Link.Save();
                }


                if ((txtSetur.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtSetur.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "SET";
                    data_Link.Save();
                }


                if ((txtTezTour.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtTezTour.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "TEZ";
                    data_Link.Save();
                }


                if ((txtVIP.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtVIP.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "VIP";
                    data_Link.Save();  
                }

                if ((txtHMS.Text.Trim() != "") && (ID > -1))
                {
                    data_Link.Add();
                    cur_Link = (DataRowView)data_Link.bs.Current;
                    cur_Link["stLink"] = txtHMS.Text.Trim();
                    cur_Link["stName"] = txtHotelName.Text;
                    cur_Link["inMasterID"] = ID;
                    cur_Link["stHotelCode"] = "HMS";
                    data_Link.Save();
                }
                kontrol = true;
                
            }
            catch 
            {
                MyTools.IRShowMessage(MyTools.ErrMsg);
                kontrol = false;
            }

            if (kontrol)
            {
                MyTools.IRShowMessage(MyTools.SaveMsg);
                Temizleme(false, false);

            }
        }

        void btnNew_Click(object sender, EventArgs e)
        {
            Temizleme(true, true);
            txtHotelName.Focus();
        }

        void btnListele_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Listele();
        }


        #region Procedureler

        void Temizleme(bool enabled, bool visible)
        {
            btnSave.Visible = visible;
            txtANI.Text = "";
            txtJOL.Text = txtANI.Text;
            txtMNG.Text = txtANI.Text;
            txtHotelName.Text = txtANI.Text;
            txtSetur.Text = txtANI.Text;
            txtTatilBudur.Text = txtANI.Text;
            txtTezTour.Text = txtANI.Text;
            txtTouristica.Text = txtANI.Text;
            txtVIP.Text = txtANI.Text;
            txtHMS.Text = txtANI.Text;
            txtANI.Enabled = enabled;
            txtMNG.Enabled = txtANI.Enabled;
            txtJOL.Enabled = txtANI.Enabled;
            txtHotelName.Enabled = txtANI.Enabled;
            txtSetur.Enabled = txtANI.Enabled;
            txtTatilBudur.Enabled = txtANI.Enabled;
            txtTezTour.Enabled = txtANI.Enabled;
            txtTouristica.Enabled = txtANI.Enabled;
            txtVIP.Enabled = txtANI.Enabled;
            txtHMS.Enabled = txtANI.Enabled;
        }

        DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            DataColumn
            col = new DataColumn("stHotelName", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stMngFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stJollyFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stAniTurFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stTTBFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stTTCFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stSETFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stTEZFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stVIPFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            col = new DataColumn("stHMSFiyat", Type.GetType("System.String"));
            dt.Columns.Add(col);
            return dt;
        }

        void Listele()
        {
            data_MNG = new IRDataSource("Select * From THotel_Link");
            //IRDataSource data_Log = new IRDataSource("Select * From THotel_Link_Log Where inID=-1");
            if (data_MNG.DB.ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in data_MNG.DB.ds.Tables[0].Rows)
                {
                    //data_Log.Add();
                    //DataRowView cur_Log = (DataRowView)data_Log.bs.Current;
                    DataRow dr = data_Grid1.NewRow();
                    dr["stHotelName"] = item["stName"];
                    dr["stMngFiyat"] = OtelFiyatAl(item["stLink"].ToString(), "MNG");
                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='JOL'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stJollyFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "JOL");
                    }
                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='ANI'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stAniTurFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "ANI");
                    }

                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='TTB'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stTTBFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "TTB");
                    }

                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='TTC'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stTTCFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "TTC");
                    }

                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='SET'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stSETFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "SET");
                    }

                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='TEZ'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stTEZFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "TEZ");
                    }

                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='VIP'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stVIPFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "VIP");
                    }

                    data_Link = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0} and stHotelCode='HMS'", item["inID"].ToString()));
                    if (data_Link.DB.ds.Tables[0].Rows.Count > 0)
                    {
                        DataRowView cur_Link = (DataRowView)data_Link.bs.Current;
                        dr["stHMSFiyat"] = OtelFiyatAl(cur_Link["stLink"].ToString(), "HMS");
                    }

                    data_Grid1.Rows.Add(dr);
                }
            }

            if (data_Grid1.Rows.Count > 0)
            {
                gridView1.Columns.Clear();
                gridControl1.DataBindings.Clear();
                gridControl1.DataSource = data_Grid1;
                MyTools.GridVisibleCols(gridView1);
                MyTools.IRSetGridColumn(gridView1, 1, "stHotelName", "Otel Adı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 2, "stMngFiyat", "MNG Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 3, "stJollyFiyat", "JOLLYTUR Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 4, "stAniTurFiyat", "ANITUR Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 5, "stTTBFiyat", "TATİLBUDUR Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 6, "stTTCFiyat", "TOURİSTİCA Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 7, "stSETFiyat", "SETUR Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 8, "stTEZFiyat", "TEZTUR Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 9, "stVIPFiyat", "VİP Fiyatı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 9, "stHMSFiyat", "HMS Fiyatı", true, 100);

            }
        
        }

        string OtelFiyatAl(string adress,string site)
        {
            bool kontrol = false;
            string Fiyat = "";
            try
            {
                wc = new WebClient();
                string webSite = wc.DownloadString(adress);
                hd = new HtmlAgilityPack.HtmlDocument();
                hd.LoadHtml(webSite);
                if (site == "MNG")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//span[@style='font-size:36px;line-height:30px;']");
                }
                else if (site == "ANI")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//td[@class='main-hotel-priceb']");
                }
                else if (site == "JOL")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//div[@id='jolly_fiyat']");
                }
                else if (site == "TTB")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//div[@class='indirimli_fiyat_son']");
                }
                else if (site == "TTC")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//strong[@id='MainContent_otelfiyati']/span");
                }
                else if (site == "SET")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//span[@id='lblSalesAmount']");
                }
                else if (site == "TEZ")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//span[@class='eb-price']");
                }
                else if (site == "VIP")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//div[@class='detay_fiyat']");
                }
                else if (site == "HMS")
                {
                    hncFiyat = hd.DocumentNode.SelectNodes("//td[@class='price']");
                }
                
                kontrol = true;
            }
            catch
            {
                kontrol = false;
            }

            if (kontrol)
            {
                if (hncFiyat != null)
                {
                    if (site == "MNG")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText.Replace("TL", "").Trim();
                            break;
                        }
                    }
                    else if (site == "JOL")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText.Replace("\n","").Replace("\r","").Replace("'den itibaren","").Replace("TL","").Trim();
                            break;
                        } 
                    }
                    else if (site == "ANI")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText;
                            break;
                        }
                    }
                    else if (site=="TTB")
                    {
                         foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText;
                            break;
                        }
                    }
                    else if (site == "TTC")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText;
                            break;
                        }
                    }
                    else if (site == "SET")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText;
                            break;
                        }
                    }
                    else if (site == "TEZ")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText.Replace("\n", "").Replace("\r", "").Replace("TL", "").Trim();
                            break;
                        }
                    }
                    else if (site == "VIP")
                    {
                        foreach (var item in hncFiyat)
                        {
                            Fiyat = item.InnerText.Replace("\n", "").Replace("\r", "").Replace("TL", "").Trim();
                            break;
                        }
                    }
                    else if (site == "HMS")
                    {
                        foreach (var item in hncFiyat)
                        {
                            string[] dizi;
                            dizi = item.InnerText.TrimStart().TrimEnd().Split(' ');
                            Fiyat = dizi[1];
                            break;
                        }
                    } 
                }
            }
            return Fiyat; 
        }

        #endregion


        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            WebClient wc = new WebClient();
            HtmlAgilityPack.HtmlDocument hd = new HtmlAgilityPack.HtmlDocument();
            string webSite = wc.DownloadString("http://www.etstur.com/Armonia-Holiday-Village-Spa");
            hd.OptionFixNestedTags = true;
            hd.LoadHtml(webSite);



            //HtmlAgilityPack.HtmlNodeCollection resim = hd.DocumentNode.SelectNodes("//div[@class='overall']/img");
            //HtmlAgilityPack.HtmlNodeCollection value = hd.DocumentNode.SelectNodes("//div[@class='overall']/em");
            //HtmlNode htmlnode = resim[0];
            //string resimdegeri = htmlnode.Attributes["src"].Value;
            //HtmlAgilityPack.HtmlNodeCollection value = hd.DocumentNode.Elements("DİV");
            

                if (hd.DocumentNode != null)
                {
                    HtmlAgilityPack.HtmlNode bodyNode = hd.DocumentNode.SelectSingleNode("//*");

                    if (bodyNode != null)
                    {
                        string deger = bodyNode.InnerHtml;
                        string deger2 = bodyNode.InnerText;
                    }
                }
            }
           
    }
}